﻿using CaroLibraly;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaroClient
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void LoadRoom()
        {
            this.panelListRoom.Controls.Clear();
            int x, y, i;
            x = 12; y = 20; i = 1;

            CaroRoomUI.CaroRoomUI crUi;

            foreach (Room room in Global.Server.ListRoom())
            {
                crUi = new CaroRoomUI.CaroRoomUI();
                crUi.Data = room;

                crUi.Location = new Point(x, y);
                crUi.Size = new Size(180, 80);

                x += (60 + crUi.Width);
                if (i++ % 3 == 0)
                {
                    y += (24 + crUi.Height);
                    x = 12;
                }
                crUi.ContextMenuStrip = this.ctmsRoom;
                crUi.DoubleClick += crUi_DoubleClick;
                this.panelListRoom.Controls.Add(crUi);
            }
        }

        void crUi_DoubleClick(object sender, EventArgs e)
        {
            CaroRoomUI.CaroRoomUI crUi = sender as CaroRoomUI.CaroRoomUI;
            if (crUi.Data.Owner.Ip == Global.Player.Ip && crUi.Data.Owner.Name == Global.Player.Name)
            {
                MessageBox.Show("Không thể chơi với chính mình!");
                return;
            }
            if (!crUi.Data.Full)
            {
                if (Global.Server.JoinRoom(crUi.Data, Global.Player))
                {
                    crUi.SetRival(Global.Player);
                    new frmPlay(crUi.Data.Owner.Ip).ShowDialog();
                }
                else
                {
                    this.LoadRoom();
                    MessageBox.Show("Phòng đầy", "Không thể tham gia!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                this.LoadRoom();
                MessageBox.Show("Phòng đầy", "Không thể tham gia!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.LoadRoom();
            this.Text = Global.Player.Name;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Global.Server.LeaveServer(Global.Player);
        }

        private void btnCreateRoom_Click(object sender, EventArgs e)
        {
            if (new frmCreateRoom().ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.LoadRoom();
                new frmPlay().ShowDialog();
                Global.Server.RemoveRoom(new Room(Global.Player, "", ""));
            }
        }

        private void btnLoadRoom_Click(object sender, EventArgs e)
        {
            this.LoadRoom();
        }

        private void btnShowRoomInfo_Click(object sender, EventArgs e)
        {
            CaroRoomUI.CaroRoomUI crUI = this.ctmsRoom.SourceControl as CaroRoomUI.CaroRoomUI;
            MessageBox.Show("Chủ phòng: " + crUI.Data.Owner.Name);
        }

        private void btnAttach_Click(object sender, EventArgs e)
        {
            CaroRoomUI.CaroRoomUI crUI = this.ctmsRoom.SourceControl as CaroRoomUI.CaroRoomUI;
            crUi_DoubleClick(crUI, e);
        }
    }
}
